package org.spawndb.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.util.Iterator;
import java.util.Properties;

public final class PropUtils {

    ////////////////////////////////////////////////////////////////////////
    public final static Properties loadProperties(final String path) throws IOException {
    	final File file                  = new File(path);
        return PropUtils.loadProperties(file);
    }
    ////////////////////////////////////////////////////////////////////////
    public final static Properties loadProperties(final File file) throws IOException {
		final Properties fileProps       = new Properties();
		if (!file.exists()) return fileProps;
	    InputStream stream = null;
        BufferedInputStream in=null;
		try {
		  stream = new FileInputStream(file);
		  in     = new BufferedInputStream(stream);
		  fileProps.load(in);
        }
        finally {
          if(in!=null) in.close();
        }
        return fileProps;
    }

    ////////////////////////////////////////////////////////////////////////
    public final static Properties storeProperties(final Properties props,
                                                   final String path) throws IOException{
    	final File file                  = new File(path);
        return PropUtils.storeProperties(props, file);
    }

    ////////////////////////////////////////////////////////////////////////
    public final static Properties storeProperties(final Properties props,
                                                   final File file) throws IOException {

		if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
		if (!file.exists()) file.createNewFile();
		BufferedOutputStream out = null;
        FileOutputStream os = null;
		try {
		  os        = new FileOutputStream(file);
		  out   = new BufferedOutputStream(os);
		  props.store(out, "Properties. Please do not edit this file.");
	    }
		finally {
          if(out!=null) out.close();
        }
        return props;
    }

    public final static Properties storeKeys(final Properties props,
                                             final File file) throws IOException{

		if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
		if (!file.exists()) file.createNewFile();

		PrintWriter out = null;
		try {
		    out   = new PrintWriter(new FileWriter(file));
		    final Iterator it = props.keySet().iterator();
		    while(it.hasNext()) out.println((String)it.next());
	    } finally {
            if(out!=null) out.close();
        }
        return props;
	}

    public static final Properties loadProperties(final HttpURLConnection connection) throws IOException {
	    BufferedInputStream in    = null;
		final Properties urlProps = new Properties();
		try {
		  final InputStream stream = connection.getInputStream();
		  in     = new BufferedInputStream(stream);
		  urlProps.load(in);
        } finally {
            if (in!=null) {
                in.close();
                connection.disconnect();  // Must do for HTTP because servers have limited connections
            }
        }
        return urlProps;
    }
}